#include "head.h"


int main(){
    const char *ip = "127.0.0.0";
    const char *port = "8080";

    Client cli;
    memset(&cli, 0, sizeof(cli));

    while(!cli.user.login){
        printf("=============== 登录系统 ==============\n");

        printf(">>>> 请输入昵称：");
        scanf("%s", cli.user.name);

        char password[OP_NAME_SIZE] = {0};
        printf(">>>> 请输入密码：");
        scanf("%s", password);

        cli.user.login = true;
    }

    system("clear");
    printf("=============== 登录成功 ==============\n");
    

    while(cli.user.login){
        int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
        struct sockaddr_in sockaddr;
        sockaddr.sin_family = AF_INET;
        sockaddr.sin_port = htons(atoi(port));
        sockaddr.sin_addr.s_addr = inet_addr(ip);
        int conn_ret = connect(socket_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
        if(conn_ret == -1){
            //todo
        }

        while(1){
            //printf("%s: ~$ ", cli.user.name);

            memset(&cli.cmd, 0, sizeof(cli.cmd));       //清空cmd
            memset(&cli.file, 0, sizeof(cli.file));     //清空file
            
            printf("%s: ~%s$ ", cli.user.name, cli.user.path);
            /*读取命令*/
            scanf(" %s",cli.cmd.cmd_arg);
            int ret = handleCommand(cli.cmd.cmd_arg, &cli.cmd.type);
            if(ret == -1){
                printf("不合法的命令，请重新输入\n");
                continue;
            }

            strcpy(cli.cmd.cmd_arg,""); //清空
            char *ptr = fgets(cli.cmd.cmd_arg, sizeof(cli.cmd.cmd_arg), stdin);
            if(ptr == NULL){
                printf("命令过长，请重新输入\n");
            }
            handleToServ(&cli, socket_fd);
        }

        close(socket_fd);
    }
    return 0;
}

